package com.example.android2019.note;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.android2019.R;

/**
 * 记录页
 */
public class RecordActivity extends AppCompatActivity implements View.OnClickListener {
    ImageView back, save, delete;
    EditText ed_content;
    NoteSQliteHelper helper;
    TextView tv_name, tv_time;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        init();
        helper = new NoteSQliteHelper(this);

        tv_name.setText("添加记录");
        Intent intent = this.getIntent();
        int id = intent.getIntExtra("id", 0);
        if (id > 0) {
            tv_name.setText("修改记录");
            tv_time.setText(intent.getStringExtra("time"));
            ed_content.setText(intent.getStringExtra("content"));
            tv_time.setVisibility(View.VISIBLE);
        }
    }

    private void init() {
        back = findViewById(R.id.note_record_image_back);
        save = findViewById(R.id.note_record_image_save);
        delete = findViewById(R.id.note_record_image_delete);
        ed_content = findViewById(R.id.note_record_ed_content);
        tv_name = findViewById(R.id.note_record_tv_name);
        tv_time = findViewById(R.id.note_record_tv_time);

        /*绑定点击事件*/
        back.setOnClickListener(this);
        save.setOnClickListener(this);
        delete.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.note_record_image_back:
                finish();
                break;
            case R.id.note_record_image_save:
                String content = ed_content.getText().toString();
                Intent intent = this.getIntent();
                int id = intent.getIntExtra("id", 1);
                if (id > 0) {
                    if (!TextUtils.isEmpty(content)) {
                        if (helper.updateDate(id, content, helper.getTime())) {
                            setResult(2);
                            finish();
                            Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(this, "修改失败", Toast.LENGTH_SHORT).show();
                        }
                    }else {
                        Toast.makeText(this, "内容不能为空", Toast.LENGTH_SHORT).show();
                    }
                } else {
                    if (!TextUtils.isEmpty(content)) {
                        if (helper.insertDate(content, helper.getTime())) {
                            setResult(2);
                            finish();
                            Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(this, "内容不能为空", Toast.LENGTH_SHORT).show();
                    }
                }
                break;
            case R.id.note_record_image_delete:
                ed_content.setText("");
                break;
            default:
                break;
        }
    }
}
